package net.alpcontrol.bi.colligation.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.alpcontrol.bi.bootstrap.bean.Message;
import net.alpcontrol.bi.bootstrap.utils.NumberFormat;
import net.alpcontrol.bi.colligation.entity.InitialCharge;
import net.alpcontrol.bi.colligation.entity.VJkPzst;
import net.alpcontrol.bi.colligation.service.IHrmResourceService;
import net.alpcontrol.bi.colligation.service.IInitialChargeService;
import net.alpcontrol.bi.colligation.service.IVJkPzstService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 丛庆
 * @since 2020-09-28
 */
@CrossOrigin
@Api(tags = {""})
@RestController
@RequestMapping("/vJkPzst")
public class VJkPzstController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private IHrmResourceService hrmResourceService;
    @Autowired
    private IVJkPzstService vJkPzstService;
    @Autowired
    private IInitialChargeService initialChargeService;

    /**
     * 查询分页数据
     */
    @ApiOperation(value = "查询分页数据")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Message<IPage<VJkPzst>> findListByPage(@RequestParam(name = "pageNum", defaultValue = "1") int pageNum, @RequestParam(name = "pageSize", defaultValue = "20") int pageSize) {
        Page<VJkPzst> page = new Page<>(pageNum, pageSize);
        IPage<VJkPzst> result = vJkPzstService.selectPage(page);
        return Message.success("查询成功", result);
    }

    /**
     * 根据id查询
     */
    @ApiOperation(value = "根据id查询数据")
    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    public Message<VJkPzst> getById(@RequestParam("id") String id) {
        return Message.success("查询成功", vJkPzstService.getById(id));
    }

    /**
     * 新增
     */
    @ApiOperation(value = "新增数据")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Message add(@RequestBody VJkPzst vJkPzst) {
        vJkPzstService.save(vJkPzst);
        return Message.success("新增成功");
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除数据")
    @RequestMapping(value = "/del", method = RequestMethod.DELETE)
    public Message delete(@RequestParam("ids") List<String> ids) {
        vJkPzstService.removeByIds(ids);
        return Message.success("删除成功");
    }

    /**
     * 修改
     */
    @ApiOperation(value = "更新数据")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Message update(@RequestBody VJkPzst vJkPzst) {
        vJkPzstService.saveOrUpdate(vJkPzst);
        return Message.success("修改成功");
    }

    /**
     * 根据月份、员工名称、公司(用于切换数据源)查询报销凭证
     *
     * @param beginMonth
     * @param endMonth
     * @param empNoStr
     * @return
     */
    @ApiOperation(value = "查询分页数据")
    @RequestMapping(value = "/listByMonthAndCompany", method = RequestMethod.GET)
    public Message<List<Map>> listByMonthAndCompany(@RequestParam(name = "beginMonth") String beginMonth,
                                                    @RequestParam(name = "endMonth") String endMonth,
                                                    @RequestParam(name = "companyId") String companyId,
                                                    @RequestParam(name = "empNo") String empNoStr) {

        List<String> empNoList = new ArrayList<>();
        if (empNoStr.length() == 0) {
            List<Map> empList = hrmResourceService.empList(companyId);
            for (int k = 0; k < empList.size(); k++) {
                empNoList.add(empList.get(k).get("loginid").toString());
            }
        }
        else{
            empNoList=Arrays.asList(empNoStr.split(","));
        }
        //查询员工初始（9月初）期初金额    0
        List<InitialCharge> initialChargeList = initialChargeService.getInitialChargeByEmp(empNoList);
        Map<String,Object> param = new HashMap<>();
        param.put("beginMonth", beginMonth);
        param.put("endMonth", endMonth);
        param.put("empNo", empNoList);
        //查询员工给定月份的期初金额
        List<Map> initialList = vJkPzstService.getInitialChargeByEmpAndMonth(param);
        //根据职员分组的初始值；
        List<Map> empInitialList = new ArrayList<>();
        for (String empNo : empNoList) {
            Float initial=0f;
            for (InitialCharge initialCharge : initialChargeList) {
                String 最初职员 = initialCharge.get职员编码();
                Float money = initialCharge.get期初金额();
                String direc = initialCharge.get期初方向();
                if(empNo.equalsIgnoreCase(最初职员)){
                    initial = (money == null || "平".equals(direc) || money == 0)?0:("贷".equals(direc) ? money : -money);
                    break;
                }
            }
            for (Map initialMap : initialList) {
                String 后期月份职员 = initialMap.get("职员编码").toString();
                Float 后期初始金额 = initialMap.containsKey("期初金额") ? Float.parseFloat(initialMap.get("期初金额").toString()):0;
                if (后期月份职员.equalsIgnoreCase(empNo)) {
                    initial += 后期初始金额;
                    break;
                }
            }
            Map<String,Object> m = new HashMap<>();
            m.put("empNo", empNo);
            m.put("initialMoney", NumberFormat.dblFromat(initial,2));
            empInitialList.add(m);
        }
        List<Map> result = vJkPzstService.listByMonthAndCompany(beginMonth, endMonth, companyId, empInitialList);
        return Message.success("0", "查询成功", result);
    }
}

